package com.sep.modules.sys.controller;


import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.sep.common.annotation.Log;
import com.sep.common.entity.QueryRequest;
import com.sep.modules.sys.entity.Test;
import com.sep.modules.sys.service.TestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("test")
public class TestController{

    @ResponseBody
    @GetMapping("/hello")
    public String test(@RequestParam(value = "name", defaultValue = "World") String name){
        return "1"+name;
    }

    @Autowired
    private TestService testService;

    private String message;

    @GetMapping
    public List<Test> testList(Test test, QueryRequest request){
        return testService.findTest(test,request).getRecords();
    }

    @Log("测试新增")
    @PostMapping
    public void addTest(@Valid Test test){
        try {
            this.testService.createTest(test);
        }catch(Exception e){
            message="新增标签失败";
            log.error(message,e);
        }
    }

    @DeleteMapping("/{testIds}")
    public void deleteTests(@NotBlank(message="{required}")@PathVariable String testIds){
        try{
            String[] ids=testIds.split(StringPool.COMMA);
            this.testService.deleteTest(ids);
        }catch(Exception e){
            message="删除标签失败";
            log.error(message,e);
        }
    }
  
    @PutMapping
    public void updateTest(Test test){
        try{
            this.testService.updateTest(test);
        }catch(Exception e){
            message="修改标签";
            log.error(message,e);
        }
    }

}
